package cn.bestick.weightrecord.controller;

import cn.bestick.weightrecord.dto.DataWrapper;
import cn.bestick.weightrecord.model.Record;
import cn.bestick.weightrecord.model.StudyRecord;
import cn.bestick.weightrecord.model.User;
import cn.bestick.weightrecord.service.RecordService;
import cn.bestick.weightrecord.service.StudyRecordService;
import cn.bestick.weightrecord.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by IntelliJ IDEA.
 * User: Bestick
 * Date: 2016/7/9
 * Time: 12:33
 * Url: http://www.bestick.cn
 * Copyright © 2015-2016 Bestick All rights reserved
 * -----------------------------------------------------------
 * 会当凌绝顶，一览众山小。
 */
@Controller
@RequestMapping("/api/record")
public class RecordController {

    @Autowired
    private RecordService recordService;

    @Autowired
    private StudyRecordService studyRecordService;

    /**
     * 添加一条记录
     *
     * @param record 页面自动封装record对象
     * @return DataWrapper
     */
    @RequestMapping("/addrecord")
    public
    @ResponseBody
    DataWrapper addRecord(HttpSession session, Record record) {
        User currentUser = (User) session.getAttribute("currentUser");
        return recordService.addRecord(currentUser, record);
    }

    /**
     * 添加一条学习记录
     *
     * @return DataWrapper
     */
    @RequestMapping("/addstudyrecord")
    public
    @ResponseBody
    DataWrapper addStudyRecord(HttpSession session, @RequestParam(value = "studyGetupTime", required = false) String studyGetupTime, @RequestParam(value = "studyGo2bedTime", required = false) String studyGo2bedTime, @RequestParam(value = "studyTodaySum", required = false) String studyTodaySum, @RequestParam(value = "userId", required = false) String userId) {
        User currentUser = null;
        StudyRecord studyRecord = null;
        try {
            currentUser = (User) session.getAttribute("currentUser");
            studyRecord = new StudyRecord();
//            //GMT+8:00的日期yyyy-MM-dd
//            String today = DateUtil.formatLong(System.currentTimeMillis(),"yyyy-MM-dd ");
            //按照GMT-5:00的时间戳存
            studyRecord.setStudyGetupTime(studyGetupTime);
            studyRecord.setStudyGo2bedTime(studyGo2bedTime);
            studyRecord.setStudyTodaySum(studyTodaySum);
            studyRecord.setUserId(Integer.parseInt(userId));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return studyRecordService.addStudyRecord(currentUser, studyRecord);
    }
}
